<?php

class Login extends Controller {
                  
    function Login()
    {
        parent::Controller();    
        $this->load->model('adminusermodel');  
        $this->_process_form();        
    }     
    
    function index()
    {

        $data["response"] = get_response();
        $data["token"] = generate_token("login",720); 

        $this->load->view("admin/default/login", $data);     

    }  

    private function _process_form()
    {

        $this->load->library("form_validation"); 

        if($this->input->post("submit_action") !== FALSE){

            if ($this->_validate_form()) {

                if (validate_token("login",$this->input->post("token",TRUE))){

                    $valid_user = $this->adminusermodel->check_user($this->input->post('username',TRUE),md5($this->input->post('password',TRUE)));

                    if (!empty($valid_user))
                    {
                        admin_session_create($valid_user);
                        set_response('success','Welcome, '.$this->session->userdata('admin_username'));
                        redirect('admin',"refresh");

                    } else {
                        set_response('error','Invalid username or password');

                    }                                                                                               

                }
            }
        }                         
    }

    private function _validate_form(){
        
        $config = array(
                        array(
                         'field'   => 'username',
                         'label'   => 'Username',
                         'rules'   => 'trim|required|xss_clean'
                        ),
                        array(
                         'field'   => 'password',
                         'label'   => 'Password',
                         'rules'   => 'trim|required|xss_clean'
                        )
                    );
        
                                                 
        $this->form_validation->set_rules($config);       
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
        
        if ($this->form_validation->run($this) == FALSE){
            return FALSE;
        }else
            return TRUE;

    }

    public function logout()
    {
        admin_session_delete();
        set_response('success',"You are logged out");
        redirect("admin/login","refresh");        
    }     
     
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */
